<?php

/**
 * 会员组模型
 * File Name：MembersGroupModel.php
 * File Encoding：UTF-8
 * File New Time：2014-5-15 16:37:52
 * Author：水平凡
 * Mailbox：admin@abc3210.com
 */
class MembersGroupModel extends BaseModel {

    //推广会员组ID
    const spreadGroupId = 3;

    public function tableName() {
        return '{{members_group}}';
    }

    public function rules() {
        return array(
            array('name', 'required', 'message' => '会员组名称不能为空！'),
            //无需验证的
            array('buffet_upgrade,starnum,icon,experience,createtime', 'safe'),
        );
    }

    public static function model($className = __CLASS__) {
        return parent::model($className);
    }

    /**
     * 获取全部用户组信息
     * @param type $options 如果是布尔值true，表示清除缓存，如果是数字id，返回对应会员组信息，否则返回全部会员组信息
     * @return array
     */
    public function getGroupList($options = '') {
        $key = 'MembersGroupModel_getGroupList';
        //如果是布尔值true，进行缓存更新
        if ((is_bool($options) && $options == true) || is_null($options)) {
            Yii::app()->cache->delete($key);
            return true;
        }
        $cache = Yii::app()->cache->get($key);
        if (empty($cache)) {
            $dataAll = $this->findAll($this->where(array('order' => 'gid desc')));
            $cache = array();
            foreach ($dataAll as $rs) {
                $cache[$rs->gid] = $rs->attributes;
            }
            //进行永久性缓存
            Yii::app()->cache->set($key, $cache);
        }
        //如果是字符串，返回指定配置
        if (is_integer($options) && $options) {
            return isset($cache[$options]) ? $cache[$options] : null;
        }
        return $cache;
    }

    /**
     * 返回 gid=>会有组名 的一个数组
     * @return type
     */
    public function getGroupArr() {
        $data = $this->getGroupList();
        if (empty($data)) {
            return array();
        }
        $groupArr = array();
        foreach ($data as $rs) {
            $groupArr[$rs['gid']] = $rs['name'];
        }
        return $groupArr;
    }

    /**
     * 获取推广用户组会员列表
     * @param type $isCache 是否刷新缓存
     * @return array
     */
    public function getSpreadMemberList($isCache = false) {
        $key = "MembersGroupModel_getSpreadMemberLists";
        //如果是布尔值true，进行缓存更新
        if ($isCache) {
            Yii::app()->cache->delete($key);
        }
        $cache = Yii::app()->cache->get($key);
        if (empty($cache)) {
            $member = MembersModel::model();
            $dataList = $member->findAll($this->where(array('group_id' => self::spreadGroupId, 'order' => 'uid desc')));
            $cache = array();
            foreach ($dataList as $rs) {
                $cache[$rs['uid']] = $rs['username'];
            }
            Yii::app()->cache->set($key, $cache, 600);
        }
        return $cache;
    }

    //数据保存前操作
    protected function beforeSave() {
        //是否新增
        if ($this->isNewRecord) {
            $this->createtime = time();
        } else {
            
        }
        $this->getGroupList(true);
        return true;
    }

}
